import SwiftUI

struct LoginView: View {
    @Environment(\.dismiss) var dismiss
    @StateObject private var viewModel: LoginViewModel
    
    init() {
        @Inject() var useCase: LoginUseCase
        _viewModel = StateObject(wrappedValue: LoginViewModel(loginUseCase: useCase))
    }
    
    var body: some View {
        NavigationView {
            VStack(spacing: 20) {
                // Header
                HStack {
                    Button(action: { dismiss() }) {
                        Image(systemName: "xmark")
    .accessibilityIdentifier("xmark")
                            .foregroundColor(.gray)
                    }
                    Spacer()
                    Image(systemName: "headphones")
                        .foregroundColor(.blue)
                }
                .padding()
                
                // Logo and Welcome
                HStack(spacing: 16) {
                    Image("logo")
                        .resizable()
                        .accessibilityIdentifier("logo")
                        .scaledToFit()
                        .frame(width: 60, height: 60)
                    
                    VStack(alignment: .leading, spacing: 4) {
                        Text("欢迎回来")
                            .accessibilityIdentifier("welcome_title")
                            .font(.title2)
                            .bold()
                        
                        Text("心存善，水润物")
                            .font(.subheadline)
                            .foregroundColor(.gray)
                    }
                    
                    Spacer()
                }
                .padding(.vertical, 20)
                .padding(.horizontal, 20)
                
                // Phone Input
                TextField("请输入手机号/身份证号", text: $viewModel.phoneNumber)
                    .padding(.horizontal)
                    .padding(.vertical, 8)
                    .overlay(
                        Rectangle()
                            .frame(height: 1)
                            .foregroundColor(Color.gray.opacity(0.15))
                            .padding(.horizontal)
                            .offset(y: 6),
                        alignment: .bottom
                    )
                
                // Terms Agreement
                HStack {
                    Button(action: { viewModel.isAgreedToTerms.toggle() }) {
                        Image(systemName: viewModel.isAgreedToTerms ? "checkmark.circle.fill" : "circle")
                            .accessibilityIdentifier("terms_checkbox")
                            .foregroundColor(viewModel.isAgreedToTerms ? .blue : .gray)
                    }
                    Text("我已阅读并同意")
                        .font(.footnote)
                    Button("《服务协议》") {
                        // Handle terms action
                    }
                    .font(.footnote)
                    .foregroundColor(.blue)
                    
                    Spacer()
                }
                .padding(.horizontal)
                
                // Error Message
                if let errorMessage = viewModel.errorMessage {
                    Text(errorMessage)
                        .foregroundColor(.red)
                        .font(.footnote)
                        .padding(.horizontal)
                }
                
                // Login Button
                Button(action: {
                    Task {
                        await viewModel.login()
                    }
                }) {
                    if viewModel.isLoading {
                        ProgressView()
                            .progressViewStyle(CircularProgressViewStyle(tint: .white))
                            .frame(maxWidth: .infinity)
                            .padding()
                            .background((viewModel.isAgreedToTerms && viewModel.isPhoneNumberValid) ? Color.blue : Color.gray.opacity(0.2))
                            .foregroundColor(.white)
                            .cornerRadius(8)
                    } else {
                        Text("注册/登录")
                            .frame(maxWidth: .infinity)
                            .padding()
                            .background((viewModel.isAgreedToTerms && viewModel.isPhoneNumberValid) ? Color.blue : Color.gray.opacity(0.2))
                            .foregroundColor(.white)
                            .cornerRadius(8)
                    }
                }
                .disabled(!(viewModel.isAgreedToTerms && viewModel.isPhoneNumberValid) || viewModel.isLoading)
                .padding(.horizontal)
                .onChange(of: viewModel.isLoginSuccessful) { newValue in
                    if newValue {
                        dismiss()
                    }
                }
                
                Spacer()
                
                // Bottom Options
                HStack {
                    Button("无障碍设置") {
                        // Handle accessibility settings
                    }
                    Spacer()
                    Button("修改登录手机号") {
                        // Handle phone number modification
                    }
                }
                .foregroundColor(.blue)
                .font(.footnote)
                .padding(.horizontal)
                
                Text("V 4.0.0.14")
                    .font(.caption)
                    .foregroundColor(.gray)
                    .padding(.bottom)
            }
        }
    }
}

#Preview {
    LoginView()
}
